/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.j002.requirementengineeringmuni.util;

import cz.muni.fi.j002.requirementengineeringmuni.Game;
import cz.muni.fi.j002.requirementengineeringmuni.entities.Move;
import java.math.BigDecimal;

/**
 *
 * @author stefan
 */
public class RuleEngine {

    /**
     * 
     * @param previousMove
     * @param currentMove
     * @return a negative value of the bet if the first player made a wrong bet, a positive value if he made a right bet or BigDecimal.ZERO if it was a tie
     */
    public static BigDecimal getFirstPlayerBetResult(Move previousMove, Move currentMove) {
        BigDecimal bet = currentMove.getFirstPlayerBet();
        if(previousMove.getCard().getValue().ordinal() < currentMove.getCard().getValue().ordinal()) {
            return currentMove.getFirstPlayerChoice().equals(Move.NextCardChoice.HIGHER) ? bet : bet.negate();
        } else if(previousMove.getCard().getValue().ordinal() > currentMove.getCard().getValue().ordinal()) {
            return currentMove.getFirstPlayerChoice().equals(Move.NextCardChoice.LOWER) ? bet : bet.negate();
        } else {
            return BigDecimal.ZERO;
        }
    }

    /**
     * 
     * @param previousMove
     * @param currentMove
     * @return a negative value of the bet if the second player made a wrong bet, a positive value if he made a right bet or BigDecimal.ZERO if it was a tie
     */
    public static BigDecimal getSecondPlayerBetResult(Move previousMove, Move currentMove) {
        BigDecimal bet = currentMove.getSecondPlayerBet();
        if(previousMove.getCard().getValue().ordinal() < currentMove.getCard().getValue().ordinal()) {
            return currentMove.getSecondPlayerChoice().equals(Move.NextCardChoice.HIGHER) ? bet : bet.negate();
        } else if(previousMove.getCard().getValue().ordinal() > currentMove.getCard().getValue().ordinal()) {
            return currentMove.getSecondPlayerChoice().equals(Move.NextCardChoice.LOWER) ? bet : bet.negate();
        } else {
            return BigDecimal.ZERO;
        }
    }

    /**
     * 
     * @param previousMove
     * @param currentMove
     * @return true if a move needs to be repeated (there was a tie), otherwise returns false
     */
    public static boolean repeatMove(Move previousMove, Move currentMove) {
        return getFirstPlayerBetResult(previousMove, currentMove).signum() == 0;
    }
    
}
